package com.gopay.common.asynexport.util;

import com.gopay.common.asynexport.dao.AsynexportDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Think on 2017/9/4.
 */
@Service("operLogTranslateHelper")
public class OperLogTranslateHelper {
    
    @Resource(name = "asynexportDao")
    private AsynexportDao asynexportDao;

    private static final String QUERY_PLATFORM_SQL = "SELECT CODE,NAME FROM dic_logx_platform";
    private static final String QUERY_CATEGORY_SQL = "SELECT CODE,NAME FROM dic_logx_category";
    private static final String QUERY_MODULES_SQL = "SELECT CODE,NAME FROM dic_logx_modules";
    private static final String QUERY_OPERTYPE_SQL = "SELECT CODE,NAME FROM dic_logx_oper_type";

    private static Map<String,Object> platformCdMap = null;
    private static Map<String,Object> categoryCdMap = null;
    private static Map<String,Object> modulesCdMap = null;
    private static Map<String,Object> operTypeCdMap = null;
    private static final byte[] LOCK = new byte[0];


    public Map<String, Object> getLogPlatformCdMap() {
        if(platformCdMap == null) {
            synchronized (LOCK) {
                if(platformCdMap == null) {
                    platformCdMap = new HashMap<>();
                    List<Object[]> list = asynexportDao.findBySql(QUERY_PLATFORM_SQL);
                    if(list != null && !list.isEmpty()) {
                        for(int i = 0;i<list.size();i++) {
                            Object[] obj = list.get(i);
                            platformCdMap.put((String) obj[0],obj[1]);
                        }
                    }
                }
            }
        }
        return platformCdMap;
    }
    
    public Map<String, Object> getLogCategoryCdMap() {
        if(categoryCdMap == null) {
            synchronized (LOCK) {
                if(categoryCdMap == null) {
                    categoryCdMap = new HashMap<>();
                    List<Object[]> list = asynexportDao.findBySql(QUERY_CATEGORY_SQL);
                    if(list != null && !list.isEmpty()) {
                        for(int i = 0;i<list.size();i++) {
                            Object[] obj = list.get(i);
                            categoryCdMap.put((String) obj[0],obj[1]);
                        }
                    }
                }
            }
        }
        return categoryCdMap;
    }
    
    public Map<String, Object> getLogModulesCdMap() {
        if(modulesCdMap == null) {
            synchronized (LOCK) {
                if(modulesCdMap == null) {
                    modulesCdMap = new HashMap<>();
                    List<Object[]> list = asynexportDao.findBySql(QUERY_MODULES_SQL);
                    if(list != null && !list.isEmpty()) {
                        for(int i = 0;i<list.size();i++) {
                            Object[] obj = list.get(i);
                            modulesCdMap.put((String) obj[0],obj[1]);
                        }
                    }
                }
            }
        }
        return modulesCdMap;
    }
    
    public Map<String, Object> getLogOperTypeMap() {
        if(operTypeCdMap == null) {
            synchronized (LOCK) {
                if(operTypeCdMap == null) {
                    operTypeCdMap = new HashMap<>();
                    List<Object[]> list = asynexportDao.findBySql(QUERY_OPERTYPE_SQL);
                    if(list != null && !list.isEmpty()) {
                        for(int i = 0;i<list.size();i++) {
                            Object[] obj = list.get(i);
                            operTypeCdMap.put((String) obj[0],obj[1]);
                        }
                    }
                }
            }
        }
        return operTypeCdMap;
    }
}
